package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDto;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    //新增品牌
    @PostMapping
    public ResponseEntity<Void> save(BrandDto brandDto) {
        brandService.saveBrand(brandDto);
        return ResponseEntity.status(HttpStatus.CREATED).build();//新增成功返回201
    }

    //根据id删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id") Long id) {
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updateBrandById(BrandDto brandDto) {
        brandService.updateBrand(brandDto);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    //根据品牌id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDto> queryBrandById(@PathVariable("id") Long brandId) {
        Brand brand = brandService.getById(brandId);
        return ResponseEntity.ok(new BrandDto(brand));
    }

    //根据品牌id集合查询品牌集合
    @GetMapping("list")
    public ResponseEntity<List<BrandDto>> queryBrandByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(BrandDto.convertEntityList(brandService.listByIds(ids)));
    }

    //分页查询品牌
    @GetMapping("page")
    public ResponseEntity<PageDTO<BrandDto>> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
    ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, key));
    }

    //根据分类id查询品牌集合
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDto>> queryBrandsByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandsByCategoryId(id));
    }


}